<?php
# Connect to database MicroWebPH
$mysqli = new mysqli("localhost", "prime", "pr1m34dm1n", "MicroWebPH");

# An error occurred while connecting to mysql
if(mysqli_connect_errno()) {
	echo 'An error occurred while connecting to the database';
}

# Remove time limit in php processing
set_time_limit(0);

# Include all necessary classes
require_once "class/account.php";
require_once "class/editor.php";
require_once "class/setting.php";
require_once "class/fpdf.php";
require_once "class/file.php";
require_once "class/resource.php";
require_once "class/location.php";
require_once "class/microorganism.php";
require_once "class/image.php";
require_once "class/document.php";
require_once "class/section.php";
require_once "class/project.php";
require_once "class/application.php";

# Process query variable
$query = empty($_REQUEST["q"])
	? (empty($_REQUEST['query'])
		? (empty($_REQUEST['id'])
			? null
			: $_REQUEST['id'])
		: $_REQUEST["query"])
	: $_REQUEST['q'];
# Get category
$_REQUEST["c"] = empty($_REQUEST["c"]) ? ((!empty($_REQUEST['view']) or !empty($_REQUEST['id'])) ? 'id' : '') : $_REQUEST["c"];
# Start of search
$start = empty($_REQUEST["s"]) ? '0' : $_REQUEST["s"];
# Current user
$user = empty($_COOKIE['acct']['id']) ? null : array('id'=>$_COOKIE['acct']['id'], 'email'=>$_COOKIE['acct']['email'], 'type'=>$_COOKIE['acct']['type']);
# End of search
$limit = empty($_REQUEST['l']) ? '10' : $_REQUEST['l'];

# Accounts
if(isset($_GET["acct"])) {
	switch($_GET["acct"]) {
		# Find account
		case "find":
			$acct = new Account($mysqli, $user);
			unset($_REQUEST['acct']);
			header('refresh: 0.1; url="http://localhost/microwebph/main.php?acct=view&q='.$query.'&'.Tool::optstrify($_REQUEST).'"'); return;
		break;
		
		# Show all accounts on page
		case "show-many":
			$acct = new Account($mysqli, $user);
			$acct->displayAll($query, $_REQUEST);
		break;
		
		# List all accounts
		case "view":
			if($user['type']!='PRIME') {
				header('refresh: 0.1; url="http://localhost/microwebph/main.php?micro=view'); return;
			}
			
			$acct = new Account($mysqli, $user);
			
			# Search and display accounts
			if(($result = $acct->search($query, $_REQUEST, $start, $limit)) and !empty($result[0]->num_rows)) {
				$acct->displayMain($result[0], $result[1], $query, $_REQUEST, $start);
			}
			# Account not found
			else if(!empty($query) and !isset($_REQUEST['fail'])) {
				header('refresh: 0.1; url="http://localhost/microwebph/main.php?acct=view&fail=view&t='.urlencode($query).'"'); return;
			}
		break;
		
		# Guess accounts
		case "guess":
			$acct = new Account($mysqli, $user);
			$acct->guess($query);
		break;
		
		# Show contents of an account
		case "show":
			if($user['type']!='PRIME') {
				header('refresh: 0.1; url="http://localhost/microwebph/main.php?micro=view'); return;
			}
			
			$acct = new Account($mysqli, $user);
			
			if($result = $acct->search($query, $_REQUEST, $start, $limit)) {
				$acct->displayContent($result[0]->fetch_object(), false);
			}
		break;
		
		# Create account
		case "make":
			if($user['type']!='PRIME') {
				header('refresh: 0.1; url="http://localhost/microwebph/main.php?micro=view'); return;
			}
			
			if(empty($_POST["type"]) or empty($_POST["email"]) or empty($_POST["pword"])
				or empty($_POST["lname"]) or empty($_POST["fname"]) or empty($_POST["mname"]) or empty($_POST["addrs"])
			) {
				# Insufficient account details
				header('refresh: 0.1; url="http://localhost/microwebph/main.php?acct=view&fail=make'); return;
			}
			
			# Create new account
			$acct = new Account($mysqli, $user);
			$id = $acct->create(
				$_POST["type"], $_POST["email"], $_POST["pword"],
				$_POST["lname"], $_POST["fname"], $_POST["mname"], $_POST["addrs"]
			);
			
			# Redirect to newly created account
			header('refresh: 0.1; url="http://localhost/microwebph/main.php?acct=view&'.(empty($id) ? 'fail=make' : 'id='.$id)); return;
		break;
		
		# Verify account credentials
		case "vrfy":
			if(empty($_POST["e"]) or empty($_POST["p"])) {
				echo 'FAILURE';
			}
			
			$acct = new Account($mysqli, $user);
			echo (($acct->verify($_POST["e"], $_POST["p"])!==false) ? 'SUCCESS' : 'FAILURE');
		break;
		
		# Modify account details
		case "edit":
			if($user['type']!='PRIME') {
				header('refresh: 0.1; url="http://localhost/microwebph/main.php?micro=view'); return;
			}
			
			# Delete account
			if(isset($_POST["del"])) {
				$acct = new Account($mysqli, $user);
				$acct->delete($_POST["del"]);
			}
			# Recover account
			if(isset($_POST["recov"])) {
				$acct = new Account($mysqli, $user);
				$acct->recover($_POST["recov"]);
			}
			else {
				# Insufficient account details
				if(empty($_POST["id"]) or empty($_POST["type"]) or !isset($_POST["report"])
					or empty($_POST["email"]) or empty($_POST["lname"]) or empty($_POST["fname"]) or empty($_POST["mname"]) or empty($_POST["addrs"])) {
					return false;
				}
				
				$acct = new Account($mysqli, $user);
				
				# Modify account details
				if($acct->update(
					$_POST["id"], $_POST["type"], $_POST["report"],
					$_POST["email"], isset($_POST["pwordNew"]) ? $_POST["pwordNew"] : null,
					$_POST["lname"], $_POST["fname"], $_POST["mname"], $_POST["addrs"]
				)) {
					return true;
				}
			}
		break;
	}
}
# Editors
elseif(isset($_GET["edtr"])) {
	switch($_GET["edtr"]) {
		# Find editor
		case "find":
			$edtr = new Editor(null, $mysqli, $user);
			unset($_REQUEST['edtr']);
			header('refresh: 0.1; url="http://localhost/microwebph/main.php?edtr=view&q='.$query.'&'.Tool::optstrify($_REQUEST).'"'); return;
		break;
		
		# Show all editors on page
		case "show-many":
			$edtr = new Editor(null, $mysqli, $user);
			$edtr->displayAll($query, $_REQUEST);
		break;
		
		# List all editors
		case "view":
			$edtr = new Editor(null, $mysqli, $user);
			
			# Search and display editors
			if(($result = $edtr->search($query, $_REQUEST, $start, $limit)) and !empty($result[0]->num_rows)) {
				$edtr->displayMain($result[0], $result[1], $query, $_REQUEST, $start);
			}
			# Editor not found
			else if(!empty($query) and !isset($_REQUEST['fail'])) {
				header('refresh: 0.1; url="http://localhost/microwebph/main.php?edtr=view&fail=view&t='.urlencode($query).'"'); return;
			}
		break;
		
		# Guess editors
		case "guess":
			$edtr = new Editor(null, $mysqli, $user);
			$edtr->guess($query);
		break;
		
		# Show contents of an editor
		case "show":
			$edtr = new Editor(null, $mysqli, $user);
			
			if($result = $edtr->search($query, $_REQUEST, $start, $limit)) {
				$edtr->displayContent($result[0]->fetch_object(), false);
			}
		break;
		
		# Create editor
		case "make":
		case "make":
			if(empty($_POST["type"]) or empty($_POST["email"]) or empty($_POST["pword"])
				or empty($_POST["lname"]) or empty($_POST["fname"]) or empty($_POST["mname"]) or empty($_POST["addrs"])
			) {
				# Insufficient editor details
				header('refresh: 0.1; url="http://localhost/microwebph/main.php?edtr=view&fail=make'); return;
			}
			
			# Create new editor
			$edtr = new Editor(null, $mysqli, $user);
			$id = $edtr->create(
				$_POST["type"], $_POST["email"], $_POST["pword"],
				$_POST["lname"], $_POST["fname"], $_POST["mname"], $_POST["addrs"]
			);
			
			# Redirect to newly created editor
			header('refresh: 0.1; url="http://localhost/microwebph/main.php?edtr=view&'.(empty($id) ? 'fail=make' : 'id='.$id)); return;
		break;
		
		# Edit editor
		case "edit":
			if(empty($_POST["id"]) or empty($_POST['lname']) or empty($_POST['fname'])) {
				# Insufficient editor details
				header('refresh: 0.1; url="http://localhost/microwebph/main.php?edtr=view&fail=make'); return;
			}
			
			# Edit new editor
			$edtr = new Editor(null, $mysqli, $user);
			$id = $edtr->update(
				$_POST["id"], $_POST["lname"], $_POST["fname"],
				isset($_POST["mname"]) ? $_POST['mname'] : '',
				isset($_POST["email"]) ? $_POST['email'] : '',
				empty($_POST["auth"]) ? '0' : '1', empty($_POST["edtr"]) ? '0' : '1'
			);
			
			if(!$id) {
				header('refresh: 0.1; url="http://localhost/microwebph/main.php?edtr=view&fail=make'); return;
			}
			
			header('refresh: 0.1; url="http://localhost/microwebph/main.php?edtr=view&id='.$id); return;
		break;
	}
}
# Microorganisms
elseif(isset($_GET["micro"])) {
	switch($_GET["micro"]) {
		# Find microorganism
		case "find":
			$micro = new Microorganism(null, $mysqli, $user);
			unset($_REQUEST['micro']);
			unset($_REQUEST['view']);
			header('refresh: 0.1; url="http://localhost/microwebph/main.php?micro=view&q='.$query.'&'.Tool::optstrify($_REQUEST).'"'); return;
		break;
		
		# List microorganisms
		case "view":
			$micro = new Microorganism(null, $mysqli, $user);
			
			if(empty($_REQUEST['q'])) {
				$_REQUEST['view'] = 1;
			}
			
			# Search and display microorganisms
			if(($result = $micro->search($query, $_REQUEST, $start, $limit)) and !empty($result[0]->num_rows)) {
				$micro->displayMain($result[0], $result[1], $query, $_REQUEST, $start);
			}
			# Microorganism is not found
			else if(!empty($query) and !isset($_REQUEST['fail'])) {
				header('refresh: 0.1; url="http://localhost/microwebph/main.php?micro=view&fail=view&t='.urlencode($query).'"'); return;
			}
		break;
		
		# Show taxonomy of microorganism
		case 'taxonomy':
			$micro = new Microorganism('taxon', $mysqli, $user);
			
			# Search taxonomy of microorganism
			if($result = $micro->search($query, $_REQUEST, $start, $limit)) {
				$micro->displayTaxa($result);
			}
		break;
		
		# Show all microorgainsms on page
		case "show-many":
			$micro = new Microorganism(null, $mysqli, $user);
			$micro->displayAll($query, $_REQUEST);
		break;
		
		# Guess microorganisms
		case "guess":
			$micro = new Microorganism(empty($_REQUEST['a']) ? null : $_REQUEST['a'], $mysqli, $user);
			$micro->guess($query);
		break;
		
		# Show microorganism's contents
		case "show":
			$micro = new Microorganism(null, $mysqli, $user);
			$_REQUEST["c"] = "id";
			
			$result = $micro->search($query, $_REQUEST, $start, $limit);
			
			# Search microorganism and show contents
			if(!empty($result[1])) {
				$micro->displayContent($result[0]->fetch_object(), false);
			}
		break;
		
		# Download microorganism details
		case 'get':
			$micro = new Microorganism(null, $mysqli, $user);
			
			if(!empty($_REQUEST['id'])) {
				$micro->download($_REQUEST['id']);
			}
		break;
		
		# Fetch microorganisms associated with a given area
		case "fetch":
			# Insufficient details passed
			if(empty($_POST["id"]) or empty($_POST["area"]) or !isset($_POST['start'])) {
				return false;
			}
			
			$micro = new Microorganism($_POST['area'], $mysqli, $user);
			
			# Fetch microorganism
			if(!$micro->fetch($_POST['id'], $_POST['start'])) {
				return false;
			}
		break;
		
		# Edit a taxonomic rank
		case "edit-taxon":
			# Insufficient details
			if(empty($_POST["id"]) or !isset($_POST["rank"]) or empty($_POST['name'])) {
				echo 'ERROR';
				return false;
			}
			
			$micro = new Microorganism('taxon', $mysqli, $user);
			
			# Modify taxon name or code
			if(!$micro->editTaxon($_POST['id'], $_POST['rank'], $_POST['name'], $_POST['code'])) {
				echo 'ERROR';
				return false;
			}
		break;
		
		# Create a new microorganism
		case "make":
			# Create microorganism inline
			if(empty($_FILES['input']['name'])) {
				# Empty scientific name, skip
				if(empty($_POST['name'])) {
					header('refresh: 0.1; url="http://localhost/microwebph/main.php?micro=view&fail=make'); return;
				}
				
				# Create microorganism
				$micro = new Microorganism(null, $mysqli, $user);
				$id = $micro->create($_POST["name"],
					empty($_POST["code"]) ? null : $_POST["code"],
					empty($_POST["author"]) ? null : $_POST["author"],
					empty($_POST["pref"]) ? null : $_POST["pref"],
					empty($_POST["prefID"]) ? null : $_POST["prefID"],
					empty($_POST["taxon"]) ? null : $_POST["taxon"],
					empty($_POST["syno"]) ? null : $_POST["syno"],
					empty($_POST["comn"]) ? null : $_POST["comn"],
					empty($_POST["host"]) ? null : $_POST["host"],
					empty($_POST["dist"]) ? null : $_POST["dist"],
					empty($_POST["notes"]) ? null : $_POST['notes'],
					empty($_POST["ref"]) ? null : $_POST['ref'],
					empty($_FILES["img"]) ? null : $_FILES['img'],
					empty($_FILES["doc"]) ? null : $_FILES['doc'],
					empty($_FILES["file"]) ? null: array('temp'=>$_FILES["file"]),
					empty($_POST["coed"]) ? null: $_POST["coed"],
					empty($_POST["categ"]) ? null : $_POST["categ"],
					empty($_POST["visib"]) ? null : $_POST["visib"]
				);
				
				# Redirect to microorganisms page
				header('refresh: 0.1; url="http://localhost/microwebph/main.php?micro=view&'.(empty($id) ? 'fail=make' : 'id='.$id)); return;
			}
			# Create via JSON inputs
			else {
				# Redirect string
				$redirect = 'refresh: 0.1; url="http://localhost/microwebph/main.php?micro=view';
				$size = count($_FILES['input']['tmp_name']);
				
				# Upload JSON files
				$micro = new Microorganism(null, $mysqli, $user);
				$results = $micro->upload($_FILES['input'], 'make', null);
				
				# One microorganism to create
				if($size==1 and $results[0]!==false) {
					$redirect .= '&id='.$results[0];
				}
				
				# Check if error occurred
				if(in_array(false, $results) or !empty($results['error'])) {
					$redirect .= '&fail=make&'.http_build_query($results['error']);
				}
				
				# Redirect to microorganisms page
				header($redirect); return;
			}
		break;
		
		# Edit microorganism details
		case "edit":
			# Delete microorganism
			if(!empty($_POST["del"])) {
				$micro = new Microorganism(null, $mysqli, $user);
				$micro->delete($_POST["del"]);
			}
			# Recover microorganism
			if(isset($_POST["recov"])) {
				$micro = new Microorganism(null, $mysqli, $user);
				$micro->recover($_POST["recov"]);
			}
			# Update microorganism
			else {
				# Update microorganism inline
				if(empty($_FILES['input']['name'])) {
					# Insufficient details passed
					if(empty($_POST["id"]) or empty($_POST["name"]) or empty($_POST["categ"]) or empty($_POST["visib"])) {
						header('refresh: 0.1; url="http://localhost/microwebph/main.php?micro=view&fail=edit'); return;
					}
					
					$micro = new Microorganism(null, $mysqli, $user);
					
					# Update microorganism
					$id = $micro->update(
						$_POST["id"], $_POST["name"],
						isset($_POST["code"]) ? $_POST["code"] : null,
						isset($_POST["author"]) ? $_POST["author"] : null,
						isset($_POST["pref"]) ? $_POST["pref"] : null,
						isset($_POST["prefID"]) ? $_POST["prefID"] : null,
						isset($_POST["taxon"]) ? $_POST["taxon"] : null,
						isset($_POST["syno"]) ? $_POST["syno"]: null,
						isset($_POST["comn"]) ? $_POST["comn"]: null,
						isset($_POST["host"]) ? $_POST["host"]: null,
						isset($_POST['notes']) ? $_POST['notes'] : null,
						isset($_POST['ref']) ? $_POST['ref'] : null,
						isset($_POST["pic"]) ? $_POST["pic"]: null,
						isset($_POST["img"]) ? $_POST["img"] : null,
						isset($_POST["doc"]) ? $_POST["doc"] : null,
						isset($_POST["loc"]) ? $_POST["loc"] : null,
						isset($_POST["rsrc"]) ? $_POST["rsrc"] : null,
						empty($_FILES["file"]) ? null: array('temp'=>$_FILES["file"]),
						isset($_POST["coed"]) ? $_POST["coed"] : null,
						isset($_POST["categ"]) ? $_POST["categ"] : null,
						isset($_POST["visib"]) ? $_POST["visib"] : null,
						isset($_POST["rsyno"]) ? $_POST["rsyno"]: null,
						isset($_POST["rcomn"]) ? $_POST["rcomn"]: null,
						isset($_POST["rhost"]) ? $_POST["rhost"]: null,
						isset($_POST["rimg"]) ? $_POST["rimg"] : null,
						isset($_POST["rmimg"]) ? $_POST["rmimg"] : null,
						isset($_POST["rdoc"]) ? $_POST["rdoc"] : null,
						isset($_POST["rloc"]) ? $_POST["rloc"] : null,
						isset($_POST["rrsrc"]) ? $_POST["rrsrc"] : null,
						isset($_POST["rcoed"]) ? $_POST["rcoed"] : null
					);
					
					# Redirect to microorganism page
					header('refresh: 0.1; url="http://localhost/microwebph/main.php?micro=view&'.($id ? 'id='.$_POST['id'] : 'fail=edit')); return;
				}
				# Update via JSON file
				else {
					# Microorganism ID is not provided, skip
					if(empty($_POST["id"])) {
						header('refresh: 0.1; url="http://localhost/microwebph/main.php?micro=view&fail=edit'); return;
					}
					
					# Redirect string
					$redirect = 'refresh: 0.1; url="http://localhost/microwebph/main.php?micro=view';
					
					# Upload JSON file and update microorganism
					$micro = new Microorganism(null, $mysqli, $user);
					$results = $micro->upload($_FILES['input'], 'edit', array('id'=>$_POST['id']));
					
					# Error occurres
					if($results[0]===false) {
						$redirect .= '&fail=edit&'.http_build_query($results['error']);
					}
					
					# Redirect page
					header($redirect); return;
				}
			}
		break;
	}
}
# Images
elseif(isset($_GET["img"])) {
	switch($_GET["img"]) {
		# Find image
		case "find":
			$img = new Image(null, null, $mysqli, $user);
			unset($_REQUEST['img']);
			header('refresh: 0.1; url="http://localhost/microwebph/main.php?img=view&q='.$query.'&'.Tool::optstrify($_REQUEST).'"'); return;
		break;
		
		# View all results
		case "view":
			$img = new Image(null, null, $mysqli, $user);
			
			if(($result = $img->search($query, $_REQUEST, $start, $limit)) and !empty($result[0]->num_rows)) {
				if($result[1]==1) {
					$img->displayMain($result[0], $result[1], $query, $_REQUEST, $start);
				}
				else {
					$img->exhibit($result[0], $result[1], $query, $_REQUEST, $start);
				}
			}
			else if(!empty($query) and !isset($_REQUEST['fail'])) {
				header('refresh: 0.1; url="http://localhost/microwebph/main.php?img=view&fail=view&t='.urlencode($query).'"'); return;
			}
		break;
		
		# Guess images
		case "guess":
			$img = new Image(null, null, $mysqli, $user);
			$img->guess($query);
		break;
		
		# Show all images on page
		case "show-many":
			$img = new Image(null, null, $mysqli, $user);
			$img->displayAll($query, $_REQUEST);
		break;
		
		# Show contents
		case "show":
			$img = new Image(null, null, $mysqli, $user);
			$_REQUEST["c"] = "id";
			
			if($result = $img->search($query, $_REQUEST, $start, $limit)) {
				$img->displayContent($result[0]->fetch_object(), false);
			}
		break;
		
		# Download image
		case 'get':
			$img = new Image(null, null, $mysqli, $user);
			
			if(!empty($_REQUEST['id'])) {
				$img->download($_REQUEST['id']);
			}
		break;
		
		# Create image
		case "make":
			if(empty($_FILES['input']['name'])) {
				if(empty($_FILES["img"])) {
					return false;
				}
				
				$img = new Image(null, null, $mysqli, $user);
				
				# Manual input
				$id = $img->create(
					$_FILES["img"],
					isset($_POST["title"]) ? $_POST['title'] : $_FILES['img']['name'],
					isset($_POST['notes']) ? $_POST['notes'] : null,
					isset($_POST['ref']) ? $_POST['ref'] : null,
					isset($_POST['mark']) ? $_POST['mark'] : null,
					isset($_POST["micro"]) ? $_POST["micro"] : null,
					isset($_POST["proj"]) ? $_POST["proj"] : null,
					isset($_POST["loc"]) ? $_POST["loc"] : null,
					isset($_POST["coed"]) ? $_POST["coed"] : null,
					isset($_POST["categ"]) ? $_POST["categ"] : null,
					isset($_POST["visib"]) ? $_POST["visib"] : null
				);
				
				header('refresh: 0.1; url="http://localhost/microwebph/main.php?img=view&'.(empty($id) ? 'fail=make' : 'id='.$id)); return;
			}
			else {
				# Json input
				$redirect = 'refresh: 0.1; url="http://localhost/microwebph/main.php?img=view';
				$size = count($_FILES['input']['tmp_name']);
				
				$img = new Image(null, null, $mysqli, $user);
				$results = $img->upload($_FILES['input'], 'make', null);
				
				if($size==1 and $results[0]!==false) {
					$redirect .= '&id='.$results[0];
				}
				
				if(in_array(false, $results) or !empty($results['error'])) {
					return false;
					$redirect .= '&fail=make&'.http_build_query($results['error']);
				}
				
				header($redirect); return;
			}
		break;
		
		# Modify image
		case "edit":
			# Delete image
			if(isset($_POST["del"])) {
				$img = new Image(null, null, $mysqli, $user);
				$img->delete($_POST["del"]);
			}
			# Recover image
			if(isset($_POST["recov"])) {
				$img = new Image(null, null, $mysqli, $user);
				$img->recover($_POST["recov"]);
			}
			# Update image
			else {
				if(empty($_POST["id"])) {
					header('refresh: 0.1; url="http://localhost/microwebph/main.php?img=view&fail=edit'); return;
				}
				
				if(empty($_FILES['input']['name'])) {
					$img = new Image(null, null, $mysqli, $user);
					
					# Manual edit
					$id = $img->update(
						$_POST["id"],
						isset($_POST["title"]) ? $_POST['title'] : 'img',
						isset($_POST['notes']) ? $_POST['notes'] : null,
						isset($_POST['ref']) ? $_POST['ref'] : null,
						isset($_POST["mark"]) ? $_POST["mark"] : null,
						isset($_POST["micro"]) ? $_POST["micro"] : null,
						isset($_POST["rsrc"]) ? $_POST["rsrc"] : null,
						isset($_POST["coed"]) ? $_POST["coed"] : null,
						isset($_POST["categ"]) ? $_POST["categ"] : null,
						isset($_POST["visib"]) ? $_POST["visib"] : null,
						isset($_POST["rmicro"]) ? $_POST["rmicro"] : null,
						isset($_POST["rmark"]) ? $_POST["rmark"] : null,
						isset($_POST["rmmicro"]) ? $_POST["rmmicro"] : null,
						isset($_POST["rmrsrc"]) ? $_POST["rmrsrc"] : null,
						isset($_POST["rrsrc"]) ? $_POST["rrsrc"] : null,
						isset($_POST["rcoed"]) ? $_POST["rcoed"] : null
					);
					
					header('refresh: 0.1; url="http://localhost/microwebph/main.php?img=view&'.($id ? 'id='.$_POST['id'] : 'fail=edit')); return;
				}
				else {
					# JSON edit
					$redirect = 'refresh: 0.1; url="http://localhost/microwebph/main.php?img=view';
					
					if(empty($_POST["id"])) {
						header($redirect.'&fail=edit'); return;
					}
					
					$img = new Image(null, null, $mysqli, $user);
					$results = $img->upload($_FILES['input'], 'edit', array('id'=>$_POST['id']));
					
					if($results[0]===false) {
						$redirect .= '&fail=edit&'.http_build_query($results['error']);
					}
				}
			}
		break;
		
		case "crop":
			$path = $_GET["path"];
			list($owidth, $oheight) = getimagesize($path);
			$ext = substr($path, strrpos($path, '.'));
			$dx = ceil($_GET["ow"]/$owidth);
			$dy = ceil($_GET["oh"]/$oheight);
			$width = ceil($_GET["w"]/$dx);
			$height = ceil($_GET["h"]/$dy);
			$x = ceil($_GET["x"]/$dx);
			$y = ceil($_GET["y"]/$dy);
			
			$imgp = imagecreatetruecolor($width, $height);
			
			switch($ext) {
				case '.jpeg':
				case '.jpg':
					header('Content-Type: image/jpeg');
					$img = imagecreatefromjpeg($path);
					imagecopy($imgp, $img, 0, 0, $x, $y, $owidth, $oheight);
					imagejpeg($imgp, null, isset($_GET['deg']) ? intval($_GET['deg']) : 20);
				break;
				
				case '.gif':
					header('Content-Type: image/gif');
					$img = imagecreatefromgif($path);
					imagecopy($imgp, $img, 0, 0, $x, $y, $owidth, $oheight);
					imagegif($imgp);
				break;
			}
			
			imagedestroy($img);
			imagedestroy($imgp);
		break;
		
		case "copy":
			list($owidth, $oheight) = getimagesize($_GET["path"]);
			$nwidth = $_GET["w"];
			$nheight = $_GET["h"];
			$nimg = imagecreatetruecolor($nwidth, $nheight);
			$path = $_GET['path'];
			
			if(filetype($path)=='image/jpeg') {
				header('Content-Type: image/jpeg');
				$oimg = imagecreatefromjpeg($path);
				imagecopyresampled($nimg, $oimg, 0, 0, 0, 0, $nwidth, $nheight, $owidth, $oheight);
				imagejpeg($nimg);
			}
			else {
				header('Content-Type: image/gif');
				$oimg = imagecreatefromgif($path);
				imagecopyresampled($nimg, $oimg, 0, 0, 0, 0, $nwidth, $nheight, $owidth, $oheight);
				imagegif($nimg);
			}
		break;
		
		case "open":
			$path = empty($_GET['path']) ? 'template/img.jpg' : $_GET['path'];
			$ext = strtolower(substr($path, strrpos($path, '.')));
			list($srcw, $srch) = getimagesize($path);
			
			if($srcw < 1000 or $srch < 1000) {
				$deg = 100;
				$div = 1;
			}
			elseif($srcw < 2000 or $srch < 2000) {
				$deg = 75;
				$div = 3;
			}
			else {
				$deg = 50;
				$div = 4;
			}
			
			$dstw = ceil($srcw/$div);
			$dsth = ceil($srch/$div);
			$imgp = imagecreatetruecolor($dstw, $dsth);
			
			switch($ext) {
				case '.jpg':
				case '.jpeg':
					header('Content-Type: image/jpeg');
					$img = imagecreatefromjpeg($path);
					imagecopyresized($imgp, $img, 0, 0, 0, 0, $dstw, $dsth, $srcw, $srch);
					imagejpeg($imgp, null, isset($_GET['deg']) ? $_GET['deg'] : $deg);
				break;
				
				case '.gif':
					header('Content-Type: image/gif');
					$img = imagecreatefromgif($path);
					imagecopyresized($imgp, $img, 0, 0, 0, 0, $dstw, $dsth, $srcw, $srch);
					imagegif($imgp, null);
				break;
				
				default:
					$this->notif[] = 'ERROR: not valid filetype';
					return false;
				break;
			}
			
			imagedestroy($img);
			imagedestroy($imgp);
		break;
		
		case "fetch":
			if(empty($_POST["id"]) or empty($_POST["area"]) or !isset($_POST['start'])) {
				return false;
			}
			
			$img = new Image($_POST['area'], null, $mysqli, $user);
			
			if(!$img->fetch($_POST['id'], $_POST['start'])) {
				return false;
			}
		break;
	}
}
# Documents
elseif(isset($_GET["doc"])) {
	switch($_GET["doc"]) {
		case "find":
			unset($_REQUEST['doc']);
			header('refresh: 0.1; url="http://localhost/microwebph/main.php?doc=view&q='.$query.'&'.Tool::optstrify($_REQUEST).'"'); return;
		break;
		
		case "view":
			$doc = new Document(null, null, $mysqli, $user);
			
			if(($result = $doc->search($query, $_REQUEST, $start, $limit)) and !empty($result[0]->num_rows)) {
				$doc->displayMain($result[0], $result[1], $query, $_REQUEST, $start);
			}
			else if(!empty($query) and !isset($_REQUEST['fail'])) {
				header('refresh: 0.1; url="http://localhost/microwebph/main.php?doc=view&fail=view&t='.urlencode($query).'"'); return;
			}
		break;
		
		# Guess documents
		case "guess":
			$doc = new Document(null, null, $mysqli, $user);
			$doc->guess($query);
		break;
		
		case "show":
			$doc = new Document(null, null, $mysqli, $user);
			$_REQUEST["c"] = "id";
			
			if($result = $doc->search($query, $_REQUEST, $start, $limit)) {
				$doc->displayContent($result[0]->fetch_object(), false);
			}
		break;
		
		case "show-many":
			$doc = new Document(null, null, $mysqli, $user);
			$doc->displayAll($query, $_REQUEST);
		break;
		
		case 'get':
			$doc = new Document(null, null, $mysqli, $user);
			
			if(!empty($_REQUEST['id'])) {
				$doc->download($_REQUEST['id']);
			}
		break;
		
		case "fetch":
			if(empty($_POST["id"]) or empty($_POST["area"]) or !isset($_POST['start'])) {
				return false;
			}
			
			$doc = new Document($_POST['area'], null, $mysqli, $user);
			
			if(!$doc->fetch($_POST['id'], $_POST['start'])) {
				return false;
			}
		break;
		
		case "make":
			if(empty($_FILES['input']['name'])) {
				if(empty($_FILES["doc"]) and empty($_POST['source'])) {
					return false;
				}
				
				$doc = new Document(null, null, $mysqli, $user);
				
				$id = $doc->create(
					isset($_FILES["doc"]) ? $_FILES["doc"] : null,
					isset($_POST["title"]) ? $_POST['title'] : $_FILES['doc']['name'],
					isset($_POST['abstr']) ? $_POST['abstr'] : null,
					isset($_POST['ref']) ? $_POST['ref'] : null,
					isset($_POST["micro"]) ? $_POST["micro"] : null,
					isset($_POST["proj"]) ? $_POST["proj"] : null,
					isset($_POST["loc"]) ? $_POST["loc"] : null,
					isset($_POST["coed"]) ? $_POST["coed"] : null,
					isset($_POST["categ"]) ? $_POST["categ"] : null,
					isset($_POST["visib"]) ? $_POST["visib"] : null
				);
				
				header('refresh: 0.1; url="http://localhost/microwebph/main.php?doc=view&'.(empty($id) ? 'fail=make' : 'id='.$id)); return;
			}
			else {
				$redirect = 'refresh: 0.1; url="http://localhost/microwebph/main.php?doc=view';
				$size = count($_FILES['input']['tmp_name']);
				
				$doc = new Document(null, null, $mysqli, $user);
				$results = $doc->upload($_FILES['input'], 'make', null);
				
				if($size==1 and $results[0]!==false) {
					$redirect .= '&id='.$results[0];
				}
				
				if(in_array(false, $results) or !empty($results['error'])) {
					return false;
					$redirect .= '&fail=make&'.http_build_query($results['error']);
				}
				
				header($redirect); return;
			}
		break;
		
		case "edit":
			if(!empty($_POST["del"])) {
				$doc = new Document(null, null, $mysqli, $user);
				$doc->delete($_POST["del"]);
			}
			if(isset($_POST["recov"])) {
				$doc = new Document(null, null, $mysqli, $user);
				$doc->recover($_POST["recov"]);
			}
			else {
				if(empty($_POST["id"])) {
					header('refresh: 0.1; url="http://localhost/microwebph/main.php?doc=view&fail=edit'); return;
				}
				
				if(empty($_FILES['input']['name'])) {
					$doc = new Document(null, null, $mysqli, $user);
					
					$id = $doc->update(
						$_POST["id"],
						isset($_POST["title"]) ? $_POST['title'] : 'doc',
						isset($_POST['abstr']) ? $_POST['abstr'] : null,
						isset($_POST['ref']) ? $_POST['ref'] : null,
						isset($_POST["micro"]) ? $_POST["micro"] : null,
						isset($_POST["rsrc"]) ? $_POST["rsrc"] : null,
						isset($_POST["coed"]) ? $_POST["coed"] : null,
						isset($_POST["categ"]) ? $_POST["categ"] : null,
						isset($_POST["visib"]) ? $_POST["visib"] : null,
						isset($_POST["rmicro"]) ? $_POST["rmicro"] : null,
						isset($_POST["rrsrc"]) ? $_POST["rrsrc"] : null,
						isset($_POST["rcoed"]) ? $_POST["rcoed"] : null
					);
					
					header('refresh: 0.1; url="http://localhost/microwebph/main.php?doc=view&'.($id ? 'id='.$_POST['id'] : 'fail=edit')); return;
				}
				else {
					$redirect = 'refresh: 0.1; url="http://localhost/microwebph/main.php?doc=view';
					
					if(empty($_POST["id"])) {
						header($redirect.'&fail=edit'); return;
					}
					
					$doc = new Document(null, null, $mysqli, $user);
					$results = $doc->upload($_FILES['input'], 'edit', array('id'=>$_POST['id']));
					
					if($results[0]===false) {
						$redirect .= '&fail=edit&'.http_build_query($results['error']);
					}
				}
			}
		break;
	}
}
# Locations
elseif(isset($_GET["loc"])) {
	switch($_GET["loc"]) {
		case "find":
			$loc = new Location(null, $mysqli, $user);
			unset($_REQUEST['loc']);
			header('refresh: 0.1; url="http://localhost/microwebph/main.php?loc=view&q='.$query.'&'.Tool::optstrify($_REQUEST).'"'); return;
		break;
		
		case "view":
			$loc = new Location(null, $mysqli, $user);
			
			if(($result = $loc->search($query, $_REQUEST, $start, $limit)) and !empty($result[0]->num_rows)) {
				$loc->displayMain($result[0], $result[1], $query, $_REQUEST, $start);
			}
			else if(!empty($query) and !isset($_REQUEST['fail'])) {
				header('refresh: 0.1; url="http://localhost/microwebph/main.php?loc=view&fail=view&t='.urlencode($query).'"'); return;
			}
		break;
		
		# Guess locations
		case "guess":
			$loc = new Location(null, $mysqli, $user);
			$loc->guess($query);
		break;
		
		# Show all locations on page
		case "show-many":
			$loc = new Location(null, $mysqli, $user);
			$loc->displayAll($query, $_REQUEST);
		break;
		
		case "show":
			$loc = new Location(null, $mysqli, $user);
			$_REQUEST["c"] = "id";
			
			if($result = $loc->search($query, $_REQUEST, $start, $limit)) {
				$loc->displayContent($result[0]->fetch_object(), false);
			}
		break;
		
		case "make":
			if(empty($_POST["name"]) or !isset($_POST["lat"]) or !isset($_POST["lng"])) {
				header('refresh: 0.1; url="http://localhost/microwebph/main.php?loc=view&fail=make'); return;
			}
			
			$loc = new Location(null, $mysqli, $user);
			
			$id = $loc->create(
				$_POST["name"], $_POST["lat"], $_POST["lng"],
				empty($_POST["gname"]) ? null : $_POST['gname'],
				isset($_POST['glat']) ? $_POST['glat'] : null,
				isset($_POST['glng']) ? $_POST['glng'] : null,
				empty($_POST["notes"]) ? null : $_POST['notes'],
				empty($_POST["ref"]) ? null : $_POST['ref'],
				empty($_POST["micro"]) ? null : $_POST['micro'],
				empty($_POST["file"]) ? null : $_POST['file'],
				empty($_POST["coed"]) ? null : $_POST['coed'],
				empty($_POST["categ"]) ? null : $_POST['categ'],
				empty($_POST["visib"]) ? null : $_POST['visib']
			);
			
			header('refresh: 0.1; url="http://localhost/microwebph/main.php?loc=view&'.(empty($id) ? 'fail=make' : 'id='.$id)); return;
		break;
		
		case "edit":
			if(isset($_POST["del"])) {
				$loc = new Location(null, $mysqli, $user);
				$loc->delete($_POST["del"]);
			}
			if(isset($_POST["recov"])) {
				$loc = new Location(null, $mysqli, $user);
				$loc->recover($_POST["recov"]);
			}
			else {
				if(empty($_POST["id"]) or empty($_POST["name"]) or !isset($_POST["lat"]) or !isset($_POST["lng"])) {
					header('refresh: 0.1; url="http://localhost/microwebph/main.php?loc=view&fail=edit'); return;
				}
				
				$loc = new Location(null, $mysqli, $user);
				
				$id = $loc->update(
					$_POST["id"], $_POST["name"], $_POST["lat"], $_POST["lng"],
					isset($_POST["gname"]) ? $_POST['gname'] : null,
					isset($_POST["glat"]) ? $_POST['glat'] : null,
					isset($_POST["glng"]) ? $_POST['glng'] : null,
					isset($_POST["notes"]) ? $_POST['notes'] : null,
					isset($_POST["ref"]) ? $_POST['ref'] : null,
					isset($_POST["micro"]) ? $_POST["micro"] : null,
					isset($_POST["img"]) ? $_POST["img"] : null,
					isset($_POST["doc"]) ? $_POST["doc"] : null,
					isset($_POST["rsrc"]) ? $_POST["rsrc"] : null,
					isset($_FILES["file"]) ? $_FILES["file"] : null,
					isset($_POST["coed"]) ? $_POST["coed"] : null,
					isset($_POST["categ"]) ? $_POST["categ"] : null,
					isset($_POST["visib"]) ? $_POST["visib"] : null,
					isset($_POST["rmicro"]) ? $_POST["rmicro"] : null,
					isset($_POST["rimg"]) ? $_POST["rimg"] : null,
					isset($_POST["rdoc"]) ? $_POST["rdoc"] : null,
					isset($_POST["rrsrc"]) ? $_POST["rrsrc"] : null,
					isset($_POST["rcoed"]) ? $_POST["rcoed"] : null
				);
				
				header('refresh: 0.1; url="http://localhost/microwebph/main.php?loc=view&'.($id ? 'id='.$_POST['id'] : 'fail=edit')); return;
			}
		break;
		
		case "fetch":
			if(empty($_POST["id"]) or empty($_POST["area"]) or !isset($_POST['start'])) {
				return false;
			}
			
			$loc = new Location($_POST['area'], $mysqli, $user);
			
			if(!$loc->fetch($_POST['id'], $_POST['start'])) {
				return false;
			}
		break;
		
		case "map":
			$loc = new Location(null, $mysqli, $user);
			
			if($result = $loc->search($query, $_REQUEST, $start, $limit)) {
				$loc->map($result[0], $result[1]);
			}
		break;
	}
}
# Resources
elseif(isset($_GET["rsrc"])) {
	switch($_GET["rsrc"]) {
		case "find":
			$rsrc = new Resource(null, $mysqli, $user);
			unset($_REQUEST['rsrc']);
			header('refresh: 0.1; url="http://localhost/microwebph/main.php?rsrc=view&q='.$query.'&'.Tool::optstrify($_REQUEST).'"'); return;
		break;
		
		case "view":
			$rsrc = new Resource(null, $mysqli, $user);
			
			if(($result = $rsrc->search($query, $_REQUEST, $start, $limit)) and !empty($result[0]->num_rows)) {
				$rsrc->displayMain($result[0], $result[1], $query, $_REQUEST, $start);
			}
			else if(!empty($query) and !isset($_REQUEST['fail'])) {
				header('refresh: 0.1; url="http://localhost/microwebph/main.php?rsrc=view&fail=view&t='.urlencode($query).'"'); return;
			}
		break;
		
		# Show all resources on page
		case "show-many":
			$rsrc = new Resource(null, $mysqli, $user);
			$rsrc->displayAll($query, $_REQUEST);
		break;
		
		# Guess resources
		case "guess":
			$rsrc = new Resource(null, $mysqli, $user);
			$rsrc->guess($query);
		break;
		
		case "show":
			$_REQUEST["c"] = "id";
			$rsrc = new Resource(null, $mysqli, $user);
			
			if($result = $rsrc->search($query, $_REQUEST, $start, $limit)) {
				$rsrc->displayContent($result[0]->fetch_object(), false);
			}
		break;
		
		case "make":
			if(empty($_POST["title"]) or empty($_POST["notes"])) {
				header('refresh: 0.1; url="http://localhost/microwebph/main.php?rsrc=view&fail=make'); return;
			}
			
			$rsrc = new Resource(null, $mysqli, $user);
			
			$id = $rsrc->create(
				$_POST["title"], $_POST["notes"],
				isset($_POST["link"]) ? $_POST["link"] : null,
				isset($_POST["ref"]) ? $_POST["ref"] : null,
				isset($_FILES["file"]) ? $_FILES["file"] : null,
				isset($_POST["coed"]) ? $_POST["coed"] : null,
				isset($_POST["categ"]) ? $_POST["categ"] : null,
				isset($_POST["visib"]) ? $_POST["visib"] : null
			);
			
			header('refresh: 0.1; url="http://localhost/microwebph/main.php?rsrc=view&'.(empty($id) ? 'fail=make' : 'id='.$id)); return;
		break;
		
		case "mark":
			if(empty($_REQUEST['area']) or empty($_POST["id"]) or empty($_POST["mark"])
				or !isset($_POST["coords"]) or empty($_POST["size"])) {
				return false;
			}
			
			$rsrc = new Resource($_REQUEST['area'], $mysqli, $user);
			
			$result = $rsrc->mark(
				isset($_GET['new']), false,
				$_POST["id"], $_POST["mark"], $_POST["coords"], $_POST["size"],
				isset($_POST["path"]) ? $_POST["path"] : null,
				isset($_POST["height"]) ? $_POST["height"] : null,
				empty($_POST["title"]) ? null : $_POST["title"],
				empty($_POST["notes"]) ? null : $_POST["notes"],
				empty($_POST["ref"]) ? null : $_POST["ref"],
				isset($_POST["categ"]) ? $_POST["categ"] : null,
				isset($_POST["visib"]) ? $_POST["visib"] : null
			);
			
			if(!$result) {
				echo $result;
			}
		break;
		
		case "draw":
			$rsrc = new Resource(null, $mysqli, $user);
			
			$result = $rsrc->draw($query);
			
			if(!$result) {
				echo $result;
			}
		break;
		
		case "focus":
			$area = isset($_REQUEST['area']) ? $_REQUEST['area'] : 'mark-home';
			$rsrc = new Resource($area, $mysqli, $user);
			
			if($area=='mark-home') {
				$list = $rsrc->focusMark(isset($_REQUEST['id']) ? $_REQUEST['id'] : null);
			}
			elseif($area=='mark-micro') {
				$list = $rsrc->focusMicro($query, isset($_REQUEST['img']) ? $_REQUEST['img'] : null);
			}
			elseif($area=='mark-img') {
				$list = $rsrc->focusImg($query, isset($_REQUEST['img']) ? $_REQUEST['img'] : null);
			}
			else {
				$list = $rsrc->focusRsrc($query, isset($_REQUEST['img']) ? $_REQUEST['img'] : null);
			}
			
			if(empty($list)) {
				return false;
			}
		break;
		
		case "share":
			if(empty($_POST["id"]) or empty($_POST["title"]) or empty($_POST["notes"])) {
				echo 'sample';
				return false;
			}
			
			$rsrc = new Resource($_POST['area'], $mysqli, $user);
			
			$rsrcID = $rsrc->share(
				$_POST["id"], $_POST["title"], $_POST["notes"],
				isset($_POST["link"]) ? $_POST["link"] : null,
				isset($_POST["ref"]) ? $_POST["ref"] : null,
				isset($_FILES["file"]) ? $_FILES["file"] : null,
				isset($_POST['coed']) ? $_POST['coed'] : null,
				isset($_POST['categ']) ? $_POST['categ'] : null,
				isset($_POST['visib']) ? $_POST['visib'] : null
			);
			
			if($rsrcID) {
				if(!empty($_FILES["file"])) {
					header('refresh: 0.1; url="http://localhost/microwebph/main.php?rsrc=view&id='.$_POST["id"].'"'); return;
				}
				else {
					echo $rsrcID;
				}
			}
			else {
				echo 'ERROR';
			}
		break;
		
		case "fetch":
			if(empty($_POST["area"]) or empty($_POST["id"]) or empty($_POST["start"])) {
				return false;
			}
			
			$rsrc = new Resource($_POST["area"], $mysqli, $user);
			
			if(!$rsrc->fetch($_POST["id"], $_POST["start"])) {
				return false;
			}
		break;
		
		case "edit":
			if(isset($_GET["mark"])) {
				$rsrc = new Resource(null, $mysqli, $user);
				
				if($_GET["mark"]=="remove-micro") {
					if(($result = $rsrc->removeMicro($_POST["m"], $query))!==true) {
						echo $result;
					}
				}
				elseif($_GET["mark"]=="remove-img") {
					if(($result = $rsrc->removeImg($_POST["m"], $query))!==true) {
						echo $result;
					}
				}
				elseif($_GET["mark"]=="remove-rsrc") {
					if(($result = $rsrc->removeRsrc($_POST["m"], $query))!==true) {
						echo $result;
					}
				}
			}
			elseif(isset($_POST["del"])) {
				$rsrc = new Resource(null, $mysqli, $user);
				$rsrc->delete($_POST["del"]);
			}
			elseif(isset($_POST["recov"])) {
				$rsrc = new Resource(null, $mysqli, $user);
				$rsrc->recover($_POST["recov"]);
			}
			else {
				if(empty($_REQUEST["q"]) or empty($_REQUEST["title"]) or empty($_REQUEST["notes"])) {
					header('refresh: 0.1; url="http://localhost/microwebph/main.php?rsrc=view&fail=edit'); return;
				}
				
				$rsrc = new Resource(null, $mysqli, $user);
				
				$id = $rsrc->update(
					$_REQUEST["q"], $_REQUEST["title"], $_REQUEST["notes"],
					isset($_POST["link"]) ? $_POST["link"] : null,
					isset($_POST["ref"]) ? $_POST["ref"] : null,
					isset($_POST["pic"]) ? $_POST["pic"] : null,
					isset($_POST["img"]) ? $_POST["img"] : null,
					isset($_POST["doc"]) ? $_POST["doc"] : null,
					isset($_FILES["file"]) ? $_FILES["file"] : null,
					isset($_POST["coed"]) ? $_POST["coed"] : null,
					isset($_POST["categ"]) ? $_POST["categ"] : null,
					isset($_POST["visib"]) ? $_POST["visib"] : null,
					isset($_POST["rlink"]) ? $_POST["rlink"] : null,
					isset($_POST["rimg"]) ? $_POST["rimg"] : null,
					isset($_POST["rdoc"]) ? $_POST["rdoc"] : null,
					isset($_POST["rcoed"]) ? $_POST["rcoed"] : null
				);
				
				header('refresh: 0.1; url="http://localhost/microwebph/main.php?rsrc=view&'.(empty($id) ? 'fail=edit' : 'id='.$_POST['id'])); return;
			}
		break;
	}
}
# Projects
elseif(isset($_GET["proj"])) {
	switch($_GET["proj"]) {
		# Find project
		case "find":
			$proj = new Project(null, $mysqli, $user);
			unset($_REQUEST['proj']);
			header('refresh: 0.1; url="http://localhost/microwebph/main.php?proj=view&q='.$query.'&'.Tool::optstrify($_REQUEST).'"'); return;
		break;
		
		# List all projects
		case "view":
			$proj = new Project(null, $mysqli, $user);
			
			if(($result = $proj->search($query, $_REQUEST, $start, $limit)) and !empty($result[0]->num_rows)) {
				$proj->displayMain($result[0], $result[1], $query, $_REQUEST, $start);
			}
			else if(!empty($query) and !isset($_REQUEST['fail'])) {
				header('refresh: 0.1; url="http://localhost/microwebph/main.php?proj=view&fail=view&t='.urlencode($query).'"'); return;
			}
		break;
		
		# Show all projects on page
		case "show-many":
			$proj = new Project(null, $mysqli, $user);
			$proj->displayAll($query, $_REQUEST);
		break;
		
		# Show project contents
		case "show":
			$proj = new Project(null, $mysqli, $user);
			$_REQUEST["c"] = "id";
			
			if($result = $proj->search($query, $_REQUEST, $start, $limit)) {
				$proj->displayContent($result[0]->fetch_object(), false);
			}
		break;
		
		# Guess projects
		case "guess":
			$proj = new Project(null, $mysqli, $user);
			$proj->guess($query);
		break;
		
		# Get all remaining projects
		case "fetch":
			if(empty($_POST["id"]) or empty($_POST["area"]) or !isset($_POST['start'])) {
				return false;
			}
			
			$proj = new Project($_POST['area'], $mysqli, $user);
			
			if(!$proj->fetch($_POST['id'], $_POST['start'])) {
				return false;
			}
		break;
		
		# Download project
		case 'get':
			$proj = new Project(null, $mysqli, $user);
			
			if(!empty($_REQUEST['id'])) {
				$proj->download($_REQUEST['id']);
			}
		break;
		
		# Create project
		case "make":
			# Create inline
			if(empty($_FILES['input']['name']) and empty($_FILES['input']['name']['multi'])) {
				if(empty($_POST["title"]) or empty($_POST["abstr"])) {
					echo 'empty!'; return;
					header('refresh: 0.1; url="http://localhost/microwebph/main.php?proj=view&fail=make'); return;
				}
				
				$proj = new Project(null, $mysqli, $user);
				$id = $proj->create(
					$_POST["title"], $_POST["abstr"],
					empty($_POST["edtr"]) ? null : $_POST["edtr"],
					empty($_POST["micro"]) ? null : $_POST["micro"],
					empty($_POST["sec"]) ? null : $_POST["sec"],
					empty($_FILES["sec"]) ? null : $_FILES["sec"],
					empty($_FILES["pimg"]) ? null : $_FILES["pimg"],
					empty($_FILES["pdoc"]) ? null : $_FILES["pdoc"],
					empty($_POST["asrce"]) ? null : $_POST["asrce"],
					empty($_POST["notes"]) ? null : $_POST["notes"],
					empty($_POST["ref"]) ? null : $_POST["ref"],
					empty($_FILES["img"]) ? null : $_FILES["img"],
					empty($_FILES["doc"]) ? null : $_FILES["doc"],
					empty($_FILES["file"]) ? null: array('temp'=>$_FILES["file"]),
					empty($_POST["coed"]) ? null: $_POST["coed"],
					empty($_POST["categ"]) ? null : $_POST["categ"],
					empty($_POST["visib"]) ? null : $_POST["visib"]
				);
				
				header('refresh: 0.1; url="http://localhost/microwebph/main.php?proj=view&'.(empty($id) ? 'fail=make' : 'id='.$id)); return;
			}
			# Via json inputs
			else {
				$proj = new Project(null, $mysqli, $user);
				$redirect = 'refresh: 0.1; url="http://localhost/microwebph/main.php?proj=view';
				
				if(empty($_FILES['input']['name']['multi'])) {
					$size = count($_FILES['input']['tmp_name']);
					$results = $proj->upload($_FILES['input'], 'make', null);
				}
				else {
					$size = count($_FILES['input']['tmp_name']['multi']);
					$results = $proj->uploadMany($_FILES['input'], 'make', null);
				}
				
				if(is_array($results)) {
					if(in_array(false, $results) or !empty($results['error'])) {
						$redirect .= '&fail=make&'.http_build_query($results['error']);
					}
				}
				else if($size==1 and $results[0]!==false) {
					$redirect .= '&id='.$results[0];
				}
				
				header($redirect); return;
			}
		break;
		
		# Modify project details
		case "edit":
			# Delete project
			if(!empty($_POST["del"])) {
				$proj = new Project(null, $mysqli, $user);
				$proj->delete($_POST["del"]);
			}
			# Recover project
			if(isset($_POST["recov"])) {
				$proj = new Project(null, $mysqli, $user);
				$proj->recover($_POST["recov"]);
			}
			else {
				# Inline
				if(empty($_FILES['input']['name'])) {
					if(empty($_POST["id"]) or empty($_POST["title"]) or empty($_POST["abstr"])) {
						header('refresh: 0.1; url="http://localhost/microwebph/main.php?proj=view&fail=edit'); return;
					}
					
					$proj = new Project(null, $mysqli, $user);
					$id = $proj->update(
						$_POST["id"], $_POST["title"], $_POST["abstr"],
						isset($_POST["edtr"]) ? $_POST["edtr"] : null,
						isset($_POST["micro"]) ? $_POST["micro"] : null,
						isset($_POST["sec"]) ? $_POST["sec"] : null,
						isset($_FILES["sec"]) ? $_FILES["sec"] : null,
						isset($_POST["asec"]) ? $_POST["asec"] : null,
						isset($_FILES["asec"]) ? $_FILES["asec"] : null,
						isset($_FILES["pdoc"]) ? $_FILES["pdoc"]: null,
						isset($_FILES["pimg"]) ? $_FILES["pimg"]: null,
						isset($_POST["img"]) ? $_POST["img"] : null,
						isset($_POST["doc"]) ? $_POST["doc"] : null,
						isset($_POST["loc"]) ? $_POST["loc"] : null,
						isset($_POST["rsrc"]) ? $_POST["rsrc"] : null,
						empty($_POST["srce"]) ? null : $_POST["srce"],
						empty($_POST["asrce"]) ? null : $_POST["asrce"],
						isset($_POST["notes"]) ? $_POST["notes"] : null,
						isset($_POST["ref"]) ? $_POST["ref"] : null,
						empty($_FILES["file"]) ? null: array('temp'=>$_FILES["file"]),
						isset($_POST["coed"]) ? $_POST["coed"] : null,
						isset($_POST["categ"]) ? $_POST["categ"] : null,
						isset($_POST["visib"]) ? $_POST["visib"] : null,
						isset($_POST["redtr"]) ? $_POST["redtr"] : null,
						isset($_POST["rmicro"]) ? $_POST["rmicro"] : null,
						isset($_POST["rsec"]) ? $_POST["rsec"] : null,
						isset($_POST["rsecimg"]) ? $_POST["rsecimg"] : null,
						isset($_POST["rsecdoc"]) ? $_POST["rsecdoc"] : null,
						isset($_POST["rimg"]) ? $_POST["rimg"] : null,
						isset($_POST["rdoc"]) ? $_POST["rdoc"] : null,
						isset($_POST["rloc"]) ? $_POST["rloc"] : null,
						isset($_POST["rrsrc"]) ? $_POST["rrsrc"] : null,
						empty($_POST["rsrce"]) ? null : $_POST["rsrce"],
						isset($_POST["rcoed"]) ? $_POST["rcoed"] : null
					);
					
					header('refresh: 0.1; url="http://localhost/microwebph/main.php?proj=view&'.($id ? 'id='.$_POST['id'] : 'fail=edit')); return;
				}
				# Json input
				else {
					$redirect = 'refresh: 0.1; url="http://localhost/microwebph/main.php?proj=view';
					
					if(empty($_POST["id"])) {
						header($redirect.'&fail=edit'); return;
					}
					
					$proj = new Project(null, $mysqli, $user);
					$results = $proj->upload($_FILES['input'], 'edit', array('id'=>$_POST['id']));
					
					if($results[0]===false) {
						$redirect .= '&fail=edit&'.http_build_query($results['error']);
					}
					
					header($redirect); return;
				}
			}
		break;
	}
}
# Settings
elseif(isset($_GET["setting"])) {
	switch($_GET["setting"]) {
		case "allow":
			if(empty($_POST["a"]) or empty($_POST["i"])) {
				return false;
			}
			
			$sett = new Setting($_POST["a"], null, $mysqli, $user);
			
			if(!$sett->allow($_POST["i"])) {
				echo "ERROR";
			}
		break;
		
		case "describe":
			if(empty($_POST["a"]) or empty($_POST["i"])) {
				return false;
			}
			
			$sett = new Setting($_POST["a"], null, $mysqli, $user);
			
			if(!$sett->describe($_POST["i"])) {
				echo "ERROR";
			}
		break;
	}
}
# Files
elseif(isset($_GET["file"])) {
	$file = new File(null, $mysqli, $user);
	switch($_GET["file"]) {
		case 'upload-one':
			if(empty($_FILES['upload'])) return false;
			
			$file->uploadOne($_FILES['upload']);
		break;
		
		case 'upload':
			if(empty($_FILES['upload'])) return false;
			
			$file->uploadMany($_FILES['upload']);
		break;
		
		case 'remove':
			if(empty($_POST['file'])) return false;
			
			$file->remove($_POST['file']);
		break;
	}
}
# Applications
elseif(isset($_GET["appn"])) {
	switch($_GET["appn"]) {
		case "find":
			$appn = new Application(null, $mysqli, $user);
			unset($_REQUEST['appn']);
			header('refresh: 0.1; url="http://localhost/microwebph/main.php?appn=view&q='.$query.'&'.Tool::optstrify($_REQUEST).'"'); return;
		break;
		
		case "view":
			if($user['type']!='PRIME') {
				header('refresh: 0.1; url="http://localhost/microwebph/main.php?micro=view'); return;
			}
			
			$appn = new Application($mysqli, $user);
			
			if(($result = $appn->search($query, $_REQUEST, $start, $limit)) and !empty($result[0]->num_rows)) {
				$appn->displayMain($result[0], $result[1], $query, $_REQUEST, $start);
			}
			else if(!empty($query) and !isset($_REQUEST['fail'])) {
				header('refresh: 0.1; url="http://localhost/microwebph/main.php?appn=view&fail=view&t='.urlencode($query).'"'); return;
			}
		break;
		
		# Guess resources
		case "guess":
			$appn = new Application(null, $mysqli, $user);
			$appn->guess($query);
		break;
		
		# Show all applications on page
		case "show-many":
			$appn = new Application(null, $mysqli, $user);
			$appn->displayAll($query, $_REQUEST);
		break;
		
		
		case "show":
			if($user['type']!='PRIME') {
				header('refresh: 0.1; url="http://localhost/microwebph/main.php?micro=view'); return;
			}
			
			$appn = new Application($mysqli, $user);
			$_REQUEST['c'] = 'id';
			
			if($result = $appn->search($query, $_REQUEST, $start, $limit)) {
				$appn->displayContent($result[0]->fetch_object(), false);
			}
		break;
		
		case "approve":
			if($user['type']!='PRIME') {
				header('refresh: 0.1; url="http://localhost/microwebph/main.php?micro=view'); return;
			}
			
			if(empty($query)) {
				return false;
			}
			
			$appn = new Application($mysqli, $user);
			$_REQUEST['c'] = 'id';
			
			if(($search = $appn->search($query, $_REQUEST, $start, $limit))
				and ($result = $appn->approve($search[0]->fetch_object()))) {
				return true;
			}
			
			echo 'ERROR';
			return false;
		break;
		
		case 'reject':
			if($user['type']!='PRIME') {
				header('refresh: 0.1; url="http://localhost/microwebph/main.php?micro=view'); return;
			}
			
			$appn = new Application($mysqli, $user);
			
			if(empty($_POST['id']) or !$appn->reject($_POST['id'])) {
				echo 'ERROR';
				return false;
			}
			
			return true;
		break;
	}
}
# Index
else {
	$img = new Image(null, null, $mysqli, $user);
	
	if(isset($_GET["index"])) {
		switch($_GET["index"]) {
			case "find":
				$_REQUEST["c"] = "guest";
				if($result = $img->search($query, $_REQUEST, $start, $limit)) {
					$img->itemize($result);
				}
			break;
			
			default:
				$img = new Image(null, null, $mysqli, $user);
				$img->present();
			break;
		}
	}
	else if(empty($_GET)) {
		$img = new Image(null, null, $mysqli, $user);
		$img->present();
	}
	else {
		echo '<b>An error occurred. Please try again later.</b>';
	}
}
























?>